package vue;

import java.awt.GridLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import controleur.ControleurToile;


public class VueAjoutClasse extends JFrame
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JButton btnval;
	private DefaultTableModel modela;
	private DefaultTableModel modelm;
	private JTable tattributs;
	private JTable tmethodes;
	private JPanel contenant;
	private JPanel title;
	private JPanel nc;
	private JPanel jt;
	private JPanel panbtnval;
	private JTextField txtnc;
	private int x;
	private int y;


	public VueAjoutClasse(ControleurToile c, int xx, int yy) {
		this.x = xx;
		this.y = yy;

		super.setTitle("Creation de Classe");
		
		contenant = new JPanel();
		contenant.setLayout(new GridLayout(2,1));
		
		String [] fieldNames = {"Nom de la classe"};

		title = new JPanel();
		title.add(new JLabel("<html> Remplir les informations ci-dessous <br/> " +
				"(appuyez sur la flèche du bas pour valider un attribut ou une méthode) </html>"));

		nc = new JPanel();
		JLabel labnc = new JLabel(fieldNames[0]);
		nc.add(labnc);
		txtnc = new JTextField(20);
		nc.add(txtnc);
		
		contenant.add(title);
		contenant.add(nc);

		jt = new JPanel();
		jt.setLayout(new GridLayout(1,2));
		
		tattributs = new JTable(1,2);
		modela = (DefaultTableModel)tattributs.getModel();
		tattributs.getColumnModel().getColumn(0).setHeaderValue("Nom Attributs");
		tattributs.getColumnModel().getColumn(1).setHeaderValue("DataType");
		String[] values = new String[]{"VARCHAR", "INT", "DECIMAL", "DOUBLE", "FLOAT"};
		TableColumn col = tattributs.getColumnModel().getColumn(1);
		col.setCellEditor(new MyComboBoxEditor(values));
		JScrollPane scroll = new JScrollPane(tattributs);
		jt.add(scroll);

		tmethodes = new JTable(1,1);
		modelm = (DefaultTableModel)tmethodes.getModel();
		tmethodes.getColumnModel().getColumn(0).setHeaderValue("Nom Methodes");
		JScrollPane scroll2 = new JScrollPane(tmethodes);
		jt.add(scroll2);

		tattributs.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e){	
				if (e.getKeyCode() == e.VK_ENTER){
					modela.addRow(new Object[]{"",""});
				}
				if(e.getKeyCode() == e.VK_DELETE){
					int x = tattributs.getSelectedRow();
					modela.removeRow(x);
				}
			}
		});

		tmethodes.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e){	
				if (e.getKeyCode() == e.VK_ENTER){
					modelm.addRow(new Object[]{"",""});
				}
				else if(e.getKeyCode() == e.VK_DELETE){
					int x = tmethodes.getSelectedRow();
					modelm.removeRow(x);
				}
			}
		});
		
		panbtnval = new JPanel();
		btnval = new JButton("Valider");
		panbtnval.add(btnval);
		btnval.addActionListener(c);
		
		super.add("North", contenant);
		super.add("Center", jt);
		super.add("South", panbtnval);
		
		super.pack();
		super.setVisible(true);
	}

	public class MyComboBoxEditor extends DefaultCellEditor {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public MyComboBoxEditor(String[] items) {
			super(new JComboBox(items));
		}
	}

	public DefaultTableModel getModela() {
		return modela;
	}

	public void setModela(DefaultTableModel modela) {
		this.modela = modela;
	}

	public DefaultTableModel getModelm() {
		return modelm;
	}

	public void setModelm(DefaultTableModel modelm) {
		this.modelm = modelm;
	}

	public JTable getTattributs() {
		return tattributs;
	}

	public void setTattributs(JTable tattributs) {
		this.tattributs = tattributs;
	}

	public JTable getTmethodes() {
		return tmethodes;
	}

	public void setTmethodes(JTable tmethodes) {
		this.tmethodes = tmethodes;
	}

	public JTextField getTxtnc() {
		return txtnc;
	}

	public void setTxtnc(JTextField txtnc) {
		this.txtnc = txtnc;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}
}